package de.uniwue.cs.ir.vsr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class CorpusImpl implements ICorpus {
	List<IDocument> corpus = new ArrayList<IDocument>();

	@Override
	public void addDocument(IDocument doc) {
		corpus.add(doc);
	}

	@Override
	public Iterator<IDocument> iterator() {
/*		for (int i=0; i<corpus.size();++i){
			System.out.println("iterator.doc: " + corpus.get(i).getId());
		}*/
		return corpus.iterator();
	}

	@Override
	public Collection<IDocument> getDocumentsContainingAll(String... terms) {
		Collection<IDocument> docs = new ArrayList<IDocument>();
		for(IDocument doc:corpus){
			if (containsAllTerms(doc, terms)){
				docs.add(doc);
			}
		}
		return docs;
	}

	@Override
	public Collection<IDocument> getDocumentsContainingAny(String... terms) {
		Collection<IDocument> docs = new ArrayList<IDocument>();
		for(IDocument doc:corpus){
			if (containsAnyTerms(doc, terms)){
				docs.add(doc);
			}
		}
		return docs;
	}

	@Override
	public int size() {
		return corpus.size();
	}
	
	private boolean containsAllTerms(IDocument doc, String[] terms) {
		for (String term:terms){
			if (doc.getTermCount(term) == 0){
				return false;
			}
		}
		return true;
	}
	
	private boolean containsAnyTerms(IDocument doc, String[] terms) {
		for (String term:terms){
			if (doc.getTermCount(term) > 0){
				return true;
			}
		}
		return false;
	}

}
